package com.zyzy.leftfourdead.fragment.groupbuy;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.zyzy.leftfourdead.R;
import com.zyzy.leftfourdead.adapter.groupBuyAdapter.GroupBuyPageAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by loverforever on 2016/8/24.
 */
public class GroupBuyPagerFragment extends Fragment {
    private Context context;
    private Toolbar toolBar_groupBuy;
    private TabLayout tabLayout_groupBuy;
    private ViewPager viewPager_groupBuy;
    private String []secondArrTitles;
    private List<Fragment>list=new ArrayList<>();
    private GroupBuyPageAdapter groupBuyPageAdapter;
    private FragmentManager fragmentManager;
    //无参数的构造方法
    public GroupBuyPagerFragment(){

    }
    //有参数的构造方法
    public GroupBuyPagerFragment( FragmentManager fragmentManager){
        this.fragmentManager=fragmentManager;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //获取上下文对象
        context=getContext();
        //获取标签
        secondArrTitles=getResources().getStringArray(R.array.secondArrTitles);
        //给list添加碎片
        list.add(new PreferenceFragment());
        list.add(new PrizeFragment());
    }


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.fragment_groupbuy,container,false);
        //初始化控件
        tabLayout_groupBuy= (TabLayout) view.findViewById(R.id.tabLayout_groupBuy);
        viewPager_groupBuy= (ViewPager) view.findViewById(R.id.viewPager_groupBuy);
        //设置左右两边加载的屏幕个数
        viewPager_groupBuy.setOffscreenPageLimit(3);
        //返回view
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        //创建适配器
        groupBuyPageAdapter=new GroupBuyPageAdapter(fragmentManager,list,secondArrTitles);
        //viewPager_groupBuy设置适配器
        viewPager_groupBuy.setAdapter(groupBuyPageAdapter);
        //让tabLayout_groupBuy与viewPager_groupBuy关联
        tabLayout_groupBuy.setupWithViewPager(viewPager_groupBuy);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }
}
